package com.sinothk.special.friendship.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sinothk.base.model.BizUserInfo;
import com.sinothk.special.friendship.entity.FriendshipLink;
import com.sinothk.special.friendship.mapper.FriendshipLinkMapper;
import com.sinothk.special.friendship.service.IFriendshipLinkService;
import com.sinothk.special.user.mapper.BizUserInfoMapper;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Description: 友谊关系维护
 * @Author: Liangyt
 * @Date: 2022-06-15
 * @Version: V1.0
 */
@Service
public class FriendshipLinkServiceImpl extends ServiceImpl<FriendshipLinkMapper, FriendshipLink> implements IFriendshipLinkService {

    @Resource
    private BizUserInfoMapper bizUserInfoMapper;

    @Override
    public void addFriend(String friendUid) {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        String mainId = loginUser.getId();
        LambdaQueryWrapper<FriendshipLink> linkLambdaQueryWrapper = new LambdaQueryWrapper<>();
        linkLambdaQueryWrapper.eq(FriendshipLink::getMainUid, mainId)
                .eq(FriendshipLink::getFriendUid, friendUid);
        FriendshipLink link = this.getOne(linkLambdaQueryWrapper);
        if (link == null) {
            link = new FriendshipLink();
        }
        link.setFriendShip(1);
        link.setMainUid(mainId);
        link.setFriendUid(friendUid);
        this.saveOrUpdate(link);

        // 更新用户信息
        new Thread(() -> {
            BizUserInfo myInfo = bizUserInfoMapper.selectById(mainId);
            BizUserInfo friendInfo = bizUserInfoMapper.selectById(friendUid);
            // 我的
            myInfo.setFollowNum(myInfo.getFollowNum() + 1);
            bizUserInfoMapper.updateById(myInfo);
            // 对方
            friendInfo.setFansNum(friendInfo.getFansNum() + 1);
            bizUserInfoMapper.updateById(friendInfo);
        }).start();
    }

    @Override
    public void delFriend(String friendUid) {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        String mainId = loginUser.getId();

        LambdaQueryWrapper<FriendshipLink> linkLambdaQueryWrapper = new LambdaQueryWrapper<>();
        linkLambdaQueryWrapper.eq(FriendshipLink::getMainUid, mainId)
                .eq(FriendshipLink::getFriendUid, friendUid);
        FriendshipLink link = this.getOne(linkLambdaQueryWrapper);
        if (link != null) {
            this.removeById(link);

            // 更新用户信息
            new Thread(() -> {
                BizUserInfo myInfo = bizUserInfoMapper.selectById(mainId);
                BizUserInfo friendInfo = bizUserInfoMapper.selectById(friendUid);
                // 我的
                long followNum = myInfo.getFollowNum() - 1;
                myInfo.setFollowNum(followNum >= 0 ? followNum : 0);
                bizUserInfoMapper.updateById(myInfo);
                // 对方
                long fansNum = friendInfo.getFansNum() - 1;
                friendInfo.setFansNum(fansNum >= 0 ? fansNum : 0);
                bizUserInfoMapper.updateById(friendInfo);
            }).start();
        }
    }

    @Override
    public void removeBlackList(String friendUid) {

    }
}
